<?php
/* 插入淘宝分类   _______________________________  */
header("Content-Type:text/html;charset=UTF-8");
require_once '../lib/functions.php';

$session = trim($_GET['top_session']);
// var_dump(getTaobaokeShop($session,'蔚蓝印'));


$params = array(  
       'method' => 'taobao.itemcats.get.v2',
       'session'=> trim($_GET['top_session']), // 主要是添加这一句 
'fields' => 'cid,parent_cid,name,is_parent,status,sort_order',
		'parent_cid' => '0',
);

$ig = new itemGet($params);
$result = $ig->getResult();
$result = $result['item_cat'];
/*for($i = 0; $i < count($result); $i++){
	foreach($result[$i] as $k => $v){
		echo '<br />'.$k.' - '.$v;
	}
	echo '<br /><hr /><br />';
	//var_dump($result[$i]);
}
//var_dump($result);*/

try{
	$dbh = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASSWORD);
	$dbh->query('set names utf8;');
	$dbh->beginTransaction();

for($i = 0; $i < count($result); $i++){
	 //  $string = "Insert INTO `categories` (`cid`, `parent_cid`, `name` , `is_parent`, `status` ,`sort_order`)
	//			VALUES(?,?,?,?,?,?)";
	  // $query=$dbh->prepare($string);
	  // $j = 1;
	  $string = "Insert INTO `categories` (`cid`, `parent_cid`, `name` , `is_parent`, `status` ,`sort_order`)
				VALUES(";
	foreach($result[$i] as $k => $v){
		if($k == 'is_parent'){
			if($v ==  'true') $v = 1;
			else $v = 0;
		}
		if($k == 'name' or $k == 'status') 
			$string = $string."'".$v."',";
		elseif($k == 'sort_order') $string = $string.$v;
		else $string = $string.$v.",";
		
	//	echo '<br />'.$j.' - '.$k.' => '.$v;
	//	$query->bindParam($j,$v);
	//	$j++;
	}
	$string = $string.")";
	echo $string.'<br />';
	$dbh->exec($string);
	//  $query->execute();     
}    
    $dbh->commit();
    echo "Succeed!";
}catch(Exception $e){
	$dbh->rollBack();
    echo "Failed: " . $e->getMessage();
}






/*
echo dirname(__FILE__);

$a = 1;
$b = 2;

$c = $a + $b;
$b .= $a;

echo $b.'<br />'.$c;
echo '<hr />';

$a1 = array('001' => 11,'002' => 22);
$a2 = array('003' => 33,'004' => 44);
$a1 = $a1 + $a2;
$a2 .= $a1;

var_dump($a1);
var_dump($a2);*/

?>